package io.netty.handler.codec.mqtt;

import java.util.ArrayList;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;

public class MyMqttEncoder {

	public static void main(String[] args) throws Exception {
		
		MqttFixedHeader fixedHeader=new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, false, 10);
		
		MqttPublishVariableHeader variableHeader=new MqttPublishVariableHeader("123456", 1);
	    
	
		ByteBuf byteBuf=PooledByteBufAllocator.DEFAULT.buffer();
		
		System.out.println(byteBuf.refCnt());
		byteBuf.writeBytes("123".getBytes());
		
		MqttPublishMessage publishMessage=
				new MqttPublishMessage(fixedHeader, variableHeader,byteBuf );
		
		ByteBuf mqttByteBuf=doEncode(PooledByteBufAllocator.DEFAULT, publishMessage);
		

		
		
		List<Object> objects=new ArrayList<Object>();
		new MqttDecoder().decode(null, mqttByteBuf, objects);
		System.out.println(objects);
		System.out.println(
				MqttEncoder.INSTANCE.acceptOutboundMessage(publishMessage));
	}
	
	public  static ByteBuf doEncode(ByteBufAllocator allocator,MqttMessage message){
		
		return MqttEncoder.doEncode(allocator, message);
	}
	
}
